package com.easemob.chat.core;

import com.easemob.util.EMLog;
import java.util.List;

class g
  implements c
{
  private static final String a = "EMHostResolver";
  private f.a b = null;
  private int c = -1;
  private int d = -1;
  private boolean e = true;
  private f.c f = null;
  private h g = null;
  private static final int h = 1;
  private int i = 1;
  private c j = null;

  void a(c paramc)
  {
    this.j = paramc;
  }

  synchronized f.c b()
  {
    if (this.f == null)
      return c();
    return this.f;
  }

  synchronized f.c c()
  {
    if (!this.g.c())
    {
      if ((this.f == null) && (this.g != null))
        this.f = this.g.a();
      return this.f;
    }
    if (this.b == null)
      this.b = f.a().h();
    if (this.b == null)
    {
      EMLog.e("EMHostResolver", "failed to get dns config");
      if (this.f == null)
      {
        this.f = new f.c();
        this.f = this.g.a();
      }
      return this.f;
    }
    f.b localb = null;
    if (this.f == null)
    {
      localb = f();
      this.f = new f.c();
      if (localb == null)
      {
        EMLog.e("EMHostResolver", "dns config did not return the ip list : " + this.b.a());
        this.e = false;
        localb = g();
      }
    }
    else
    {
      if (this.e)
        localb = f();
      if (localb == null)
      {
        this.e = false;
        localb = g();
      }
      if (localb == null)
        if (this.i <= 0)
        {
          this.f = this.g.a();
          this.i = 1;
        }
        else
        {
          this.i -= 1;
          f.a locala = f.a().j();
          if (locala != null)
          {
            if (this.b != locala)
            {
              this.b = locala;
              if (this.j != null)
                this.j.a();
            }
            this.e = true;
            this.c = -1;
            this.d = -1;
            localb = f();
            if (localb == null)
              localb = g();
          }
        }
    }
    if (localb != null)
    {
      this.f.a = (this.e ? localb.b : localb.a);
      this.f.b = localb.c;
      this.f.c = localb.d;
      this.f.d = localb;
    }
    else
    {
      this.e = true;
      this.c = -1;
      this.d = -1;
      this.f = this.g.a();
    }
    if (this.f != null)
      EMLog.d("EMHostResolver", "the next availabe host : " + this.f.toString());
    else
      EMLog.e("EMHostResolver", "no availabe host is selected, we should not reach here!!");
    return this.f;
  }

  int d()
  {
    List localList = this.g.b();
    return (localList != null) && (localList.size() > 0) ? localList.size() * 2 - (this.c + 1) - (this.d + 1) : 0;
  }

  void e()
  {
    this.b = null;
    this.f = null;
    this.c = -1;
    this.d = -1;
    this.e = true;
  }

  private void a(f.a parama)
  {
    if ((parama != null) && (parama != this.b))
      this.b = parama;
  }

  private f.b f()
  {
    if (this.b != null)
    {
      List localList = this.g.b();
      this.c += 1;
      if ((localList != null) && (this.c < localList.size()))
        while (this.c < localList.size())
        {
          f.b localb = a(this.c);
          if ((localb != null) && (localb.b != null) && (!localb.b.trim().equals("")))
            return localb;
          this.c += 1;
        }
    }
    this.c = -1;
    return null;
  }

  private f.b g()
  {
    if (this.b != null)
    {
      List localList = this.g.b();
      this.d += 1;
      if ((localList != null) && (this.d < localList.size()))
        while (this.d < localList.size())
        {
          f.b localb = a(this.d);
          if ((localb != null) && (localb.a != null) && (!localb.a.trim().equals("")))
            return localb;
          this.d += 1;
        }
    }
    this.d = -1;
    return null;
  }

  private f.b a(int paramInt)
  {
    if (this.b != null)
    {
      List localList = this.g.b();
      if (localList == null)
        return null;
      int k = localList.size();
      if ((k > 0) && (paramInt < k))
        return (f.b)localList.get(paramInt);
    }
    return null;
  }

  void a(h paramh)
  {
    this.g = paramh;
  }

  public void a()
  {
    if ((this.b != null) && (this.b != f.a().h()))
    {
      e();
      a(f.a().h());
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.core.g
 * JD-Core Version:    0.6.1
 */